package com.gdkf.dormitory.tools;

import com.alibaba.fastjson.JSONObject;
import com.gdkf.dormitory.entity.*;

public class MyPageTool {
//  该方法进行第一次解析，解析得到对象的JSON数据的object和当前页等数据
    public static MyPage parseToPage(JSONObject templateObject){
        MyPage objectMyPage = new MyPage();
        JSONObject getPageTemplateJson = (JSONObject)JSONObject.parse(templateObject.get("page").toString());
        try{
            objectMyPage.setPageTotal(Long.parseLong(getPageTemplateJson.get("pageTotal").toString()));
            objectMyPage.setCurrentPage(Long.parseLong(getPageTemplateJson.get("currentPage").toString()));
            objectMyPage.setSizePage(Long.parseLong(getPageTemplateJson.get("sizePage").toString()));
        }catch (NullPointerException e){
            if(getPageTemplateJson.get("pageTotal")==null){
                objectMyPage.setPageTotal(0);
            }
            if(getPageTemplateJson.get("currentPage") == null){
                objectMyPage.setCurrentPage(0);
            }
            if(getPageTemplateJson.get("sizePage") == null ){
                objectMyPage.setSizePage(0);
            }

        }

        //如果获得的对象数据是空，那么我就给个空给它，以便下一步判断
        try {
            if(templateObject.get("object")==null){
                objectMyPage.setObject(null);
                return objectMyPage;
            }else{
                objectMyPage.setObject(templateObject.get("object").toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return objectMyPage;

    }

    public static MyPage JsonParseToStudent(MyPage myPage){
        System.out.println("=JsonParseToStudent===myPage==>"+myPage);
        JSONObject studentByJson = (JSONObject)JSONObject.parse(myPage.getObject().toString());
        Student student = new Student();
        student.setStuId(studentByJson.get("stuId").toString());
        student.setStuName(studentByJson.get("stuName").toString());
        myPage.setObject(student);
        return myPage;
    }
    public static MyPage JsonParseToDorm(MyPage myPage){
        System.out.println("=JsonParseToDorm===myPage==>"+myPage);
        JSONObject dormByJson = (JSONObject)JSONObject.parse(myPage.getObject().toString());
        Dorm dorm = new Dorm();
        dorm.setHousId(dormByJson.get("housId").toString());
        dorm.setDormId(dormByJson.get("dormId").toString());
        myPage.setObject(dorm);
        return myPage;
    }
    public static MyPage JsonParseToEmp(MyPage myPage){
        System.out.println("=JsonParseToDorm===myPage==>"+myPage);
        JSONObject empByJson = (JSONObject)JSONObject.parse(myPage.getObject().toString());
        Emp emp = new Emp();
        emp.setEmpId(empByJson.get("empId").toString());
        emp.setEmpName(empByJson.get("empName").toString());
        myPage.setObject(emp);
        return myPage;
    }

    public static MyPage JsonParseToMaintenance(MyPage myPage){
        System.out.println("=JsonParseToDorm===myPage==>"+myPage);
        JSONObject maintenanceByJson = (JSONObject)JSONObject.parse(myPage.getObject().toString());
        Maintenance maintenance = new Maintenance();
        maintenance.setMainId(Integer.parseInt(maintenanceByJson.get("mainId").toString()));
        maintenance.setHousId(maintenanceByJson.get("housId").toString());
        maintenance.setDormId(maintenanceByJson.get("dormId").toString());
        myPage.setObject(maintenance);
        return myPage;
    }
    public static MyPage JsonParseToLate(MyPage myPage){
        System.out.println("=JsonParseToDorm===myPage==>"+myPage);
        JSONObject lateByJson = (JSONObject)JSONObject.parse(myPage.getObject().toString());
        Late late = new Late();
        late.setDormId(lateByJson.get("dormId").toString());
        late.setHousId(lateByJson.get("housId").toString());
        late.setStuId(lateByJson.get("stuId").toString());
        System.out.println(late);
        myPage.setObject(late);
        return myPage;
    }

    public static MyPage JsonParseToRepair(MyPage myPage){
        System.out.println("=JsonParseToDorm===myPage==>"+myPage);
        JSONObject repairByJson = (JSONObject)JSONObject.parse(myPage.getObject().toString());
        Repair repair = new Repair();
        try {
            repair.setRepId(Integer.parseInt(repairByJson.get("repId").toString()));
        } catch (NullPointerException e) {
            repair.setRepId(null);
        }
        repair.setHousId(repairByJson.get("housId").toString());
        repair.setDormId(repairByJson.get("dormId").toString());
        repair.setStuId(repairByJson.get("stuId").toString());
        myPage.setObject(repair);
        return myPage;
    }
    public static MyPage JsonParseToVisit(MyPage myPage){
        System.out.println("=JsonParseToDorm===myPage==>"+myPage);
        JSONObject visitByJson = (JSONObject)JSONObject.parse(myPage.getObject().toString());
        Visit visit = new Visit();
        visit.setHousId(visitByJson.get("housId").toString());
        visit.setDormId(visitByJson.get("dormId").toString());
        visit.setViName(visitByJson.get("viName").toString());
        myPage.setObject(visit);
        return myPage;
    }
    public static MyPage JsonParseToAdmin(MyPage myPage){
        System.out.println("=JsonParseToDorm===myPage==>"+myPage);
        JSONObject adminByJson = (JSONObject)JSONObject.parse(myPage.getObject().toString());
        Admin admin = new Admin();
        admin.setAId(Integer.parseInt(adminByJson.get("aId").toString()));
        admin.setUsername(adminByJson.get("userName").toString());
        myPage.setObject(admin);
        return myPage;
    }
    public static MyPage JsonParseToRole(MyPage myPage){
        System.out.println("=JsonParseToDorm===myPage==>"+myPage);
        JSONObject roleByJson = (JSONObject)JSONObject.parse(myPage.getObject().toString());
        Role role = new Role();
        role.setTypeName(roleByJson.get("rid").toString());
        role.setTypeName(roleByJson.get("typeName").toString());
        myPage.setObject(role);
        return myPage;
    }

    public static MyPage JsonParseToZhu(MyPage myPage){
        System.out.println("=JsonParseToDorm===myPage==>"+myPage);
        JSONObject zhuByJson = (JSONObject)JSONObject.parse(myPage.getObject().toString());
        Zhu zhu = new Zhu();
        zhu.setZhuId(Integer.parseInt(zhuByJson.get("zhuId").toString()));
        zhu.setStuId(zhuByJson.get("stuId").toString());
        myPage.setObject(zhu);
        return myPage;
    }
}
